home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Taifun / Taifun 009 (1987-02-15)(Ossowski, Stefan)(DE)(PD).zip / Taifun 009 (1987-02-15)(Ossowski, Stefan)(DE)(PD).adf / HackIconII_Source / HackIcon.c < prev    next >
C/C++ Source or Header  |  1987-03-04  |  12KB  |  473 lines

  1. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
  2. * |_o_o|\\ Copyright (c) 1987 The Software Distillery.  All Rights Reserved *
  3. * |. o.| ||          Written by Doug Walker                                 *
  4. * | .  | ||          The Software Distillery                                *
  5. * | o  | ||          235 Trillingham Lane                                   *
  6. * |  . |//           Cary, NC 27511                                         *
  7. * ======             BBS:(919)-471-6436                                     *
  8. \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  9.  
  10. #include "mydebug.h"
  11.  
  12. #ifdef MYDEBUG
  13. int debug = 0;
  14. #endif
  15.  
  16. #include "exec/types.h"
  17. #include "exec/memory.h"
  18. #include "intuition/intuition.h"
  19. #include "graphics/gfxbase.h"
  20. #include "stdio.h"
  21.  
  22. #include "hackicon.h"
  23.  
  24. extern struct IntuitionBase *IntuitionBase;
  25. extern struct GfxBase *GfxBase;
  26.  
  27. extern struct Menu Titles[];
  28. extern struct Gadget colgads[];
  29. extern struct IntuiText AboutText[];
  30. extern char TextBuffer[];
  31. extern struct Requester NameRequester;
  32. extern struct Gadget Req_gads[];
  33. extern struct Border Req_Bords[];
  34. extern struct IntuiText Load_text, Save_text;
  35.  
  36. struct Screen   *Screen;
  37. struct Window   *Window;
  38. struct ViewPort *ViewPort;
  39.  
  40. struct iconfont *fonts[2];
  41. int fontnum = 0;
  42. int hires = 0;
  43.  
  44. USHORT colormap[16] = {0x0000, 0x000f, 0x00f0, 0x0f00,
  45.                        0x00ff, 0x0f0f, 0x0ff0, 0x0fff,
  46.                        0x0000, 0x000f, 0x00f0, 0x0f00,
  47.                        0x00ff, 0x0f0f, 0x0ff0, 0x0fff
  48.                       };
  49.  
  50.  
  51. init()
  52. {
  53.    struct NewScreen *NewScreen;
  54.    struct NewWindow *NewWindow;
  55.  
  56.      BUG(1, ("init: enter\n"))
  57.  
  58.    if(!(NewScreen = (struct NewScreen *)
  59.             AllocMem(sizeof(struct NewScreen), MEMF_CHIP | MEMF_CLEAR))
  60.       || !(NewWindow = (struct NewWindow *)
  61.             AllocMem(sizeof(struct NewWindow), MEMF_CHIP | MEMF_CLEAR))
  62.      )
  63.       panic("Not enough memory for screens!\n");
  64.  
  65.    if(! (IntuitionBase = (struct IntuitionBase *)
  66.               OpenLibrary("intuition.library", I_REV)) )
  67.       panic("Can't open intuition.library!\n");
  68.  
  69.    if(! (GfxBase = (struct GfxBase *)
  70.               OpenLibrary("graphics.library", I_REV)) )
  71.       panic("Can't open graphics.library!\n");
  72.  
  73.  
  74.    NewScreen->Width  = SWIDTH;
  75.    NewScreen->Height = SHEIGHT;
  76.    NewScreen->Depth  = SDEPTH;
  77.    NewScreen->DetailPen = 1;
  78.    NewScreen->BlockPen  = 2;
  79.    NewScreen->ViewModes = HIRES;
  80.    NewScreen->Type   = CUSTOMSCREEN;
  81.    NewScreen->DefaultTitle  = (UBYTE *)"HackIcon II - Copyright (c) 1987 The Software Distillery";
  82.  
  83.    Screen = (struct Screen *) OpenScreen(NewScreen);
  84. BUG(9, ("init: Screen open\n"))
  85.  
  86.    FreeMem(NewScreen, sizeof(struct NewScreen));
  87. BUG(9, ("init: newscreen mem freed\n"))
  88.  
  89.    NewWindow->Width  = SWIDTH;
  90.    NewWindow->Height = SHEIGHT;
  91.    NewWindow->DetailPen = 1;
  92.    NewWindow->BlockPen  = 2;
  93.    NewWindow->IDCMPFlags  = (MOUSEBUTTONS  | MENUPICK | GADGETUP);
  94.    NewWindow->Flags = (SMART_REFRESH | BACKDROP | BORDERLESS | ACTIVATE);
  95.    NewWindow->Title = NULL;
  96.    NewWindow->Screen = Screen;
  97.    NewWindow->MinWidth  =
  98.    NewWindow->MaxWidth  = SWIDTH;
  99.    NewWindow->MinHeight =
  100.    NewWindow->MaxHeight = SHEIGHT;
  101.    NewWindow->Type = CUSTOMSCREEN;
  102.  
  103.    Window = (struct Window *)OpenWindow(NewWindow);
  104. BUG(9, ("init: Window open\n"))
  105.  
  106.    FreeMem(NewWindow, sizeof(struct NewWindow));
  107. BUG(9, ("init: newwindow mem freed\n"))
  108.  
  109.    ViewPort = (struct ViewPort *)ViewPortAddress(Window);
  110.  
  111.    LoadRGB4(ViewPort, colormap, 16);
  112.  
  113.    DisplayLabels();
  114.  
  115. BUG(9, ("init: setting menu labels\n"))
  116. BUGGETC(9)
  117.    SetMenuStrip(Window, Titles);
  118.  
  119.    BUG(1, ("init: exit\n"))
  120.    return(0);
  121. }
  122.  
  123. clean(forced)
  124. int forced;
  125. {
  126.    int i;
  127.  
  128.    BUG(1, ("clean: Enter\n"))
  129.  
  130.    if(!forced)
  131.    {
  132.       for(i=0; i<MAXFONTS && fonts[i]; i++)
  133.       {
  134.          if(fonts[i]->flags & CHANGED)
  135.          {
  136.             AboutText[8].IText = fonts[i]->filename;
  137.             if(!AutoRequest(Window, &AboutText[8], &AboutText[11], 
  138.                &AboutText[12], 0, 0, 430, 90))
  139.                return(1);
  140.          }
  141.       }
  142.    }
  143.    CloseWindow(Window);
  144.    CloseScreen(Screen);
  145.    CloseLibrary(GfxBase);
  146.    CloseLibrary(IntuitionBase);
  147.  
  148.    for(i=0; i<MAXFONTS && fonts[i]; i++)
  149.       FreeMem(fonts[i], sizeof(struct iconfont));
  150.  
  151.    BUG(1, ("clean: Exit\n"))
  152.  
  153.    return(0);
  154. }
  155.  
  156. char *GetName(load)
  157. int load;
  158. {
  159.    ULONG Class;
  160.    USHORT Code;
  161.    struct Gadget *Gadget;
  162.    struct IntuiMessage *imsg;
  163.  
  164.    BUG(1, ("GetName: Enter\n"))
  165.    
  166.    if(!load && fonts[fontnum]) strcpy(TextBuffer, fonts[fontnum]->filename);
  167.    else strcpy(TextBuffer, "Characters");
  168.  
  169.    InitRequester(&NameRequester);
  170.    NameRequester.LeftEdge = 10;
  171.    NameRequester.TopEdge = 20;
  172.    NameRequester.Width = 220;
  173.    NameRequester.Height = 90;
  174.    NameRequester.ReqGadget = Req_gads;
  175.    NameRequester.ReqBorder = Req_Bords;
  176.    NameRequester.ReqText = (load ? &Load_text : &Save_text);
  177.    NameRequester.BackFill = 1;
  178.  
  179.    Request(&NameRequester, Window);
  180.  
  181.    while(1)
  182.    {
  183.       if(!(imsg = (struct IntuiMessage *)GetMsg(Window->UserPort)))
  184.       {
  185.          WaitPort(Window->UserPort);
  186.          continue;
  187.       }
  188.       Class  = imsg->Class;
  189.       Code   = imsg->Code;
  190.       Gadget = (struct Gadget *)imsg->IAddress;
  191.       ReplyMsg(imsg);
  192. BUG(1, ("GetName: Got message, Class=%d, Code=%d\n", Class, Code))
  193.  
  194.       if(Class == GADGETUP)
  195.       {
  196. BUG(1, ("GetName: GADGETUP, GadgetID=%d\n", Gadget->GadgetID))
  197.          if(Gadget->GadgetID == G_RCANCEL)
  198.             return(NULL);
  199.          else if(Gadget->GadgetID == G_ROK)
  200.             return(TextBuffer[0] ? TextBuffer : NULL);
  201.       }
  202.    }
  203.    
  204.    return(NULL);
  205. }
  206.  
  207. WhichIcon(x, y)
  208. short x, y;
  209. {
  210.    int row, col;
  211.  
  212.    BUG(1, ("WhichIcon: Enter, pos=(%d, %d)\n", (int)x, (int)y))
  213.  
  214.    if((y-IMYTOP)%IMYSKIP<8) row = (y - IMYTOP) / IMYSKIP;
  215.    else
  216.    {
  217.       BUG(5, ("WhichIcon: bad row, returning -1\n"))
  218.       return(-1);
  219.    }
  220.  
  221.    if((x-IMXLEFT)%IMXSKIP<8) col = (x - IMXLEFT) / IMXSKIP;
  222.    else
  223.    {
  224.       BUG(5, ("WhichIcon: bad col, returning -1\n"))
  225.       return(-1);
  226.    }
  227.  
  228.    BUG(5, ("WhichIcon: Exit, row %d col %d returning %d", row, col, row*IMNCOLS + col))
  229.  
  230.    return(row*IMNCOLS + col);
  231. }
  232.  
  233. SetFont()
  234. {
  235.    int start;
  236.    start = fontnum;
  237.    while(1)
  238.    {
  239.       if(++fontnum >= MAXFONTS) fontnum = 0;
  240.       if(fontnum == start)
  241.       {
  242.          DisplayBeep((struct SCREEN *)NULL);
  243.          return(1);
  244.       }
  245.       if(fonts[fontnum]) break;
  246.    };
  247.    DrawImage(Window->RPort, fonts[fontnum]->images, 0, 0);
  248.    SetWindowTitles(Window, fonts[fontnum]->filename, fonts[fontnum]->filename);
  249.  
  250.    return(0);
  251. }
  252.  
  253. AboutHackicon()
  254. {
  255.    if(!AutoRequest(Window, AboutText, &AboutText[5], &AboutText[6], 0, 0, 
  256.                410, 90))
  257.       while(!AutoRequest(Window, &AboutText[7], &AboutText[5], &AboutText[6],
  258.             0, 0, 410, 90));
  259.    return(0);
  260. }
  261.  
  262. USHORT heximagedata[] =
  263. {
  264.   0x6000, 0x9000, 0x9000, 0x9000, 0x6000,   /* 0 */
  265.   0x4000, 0xc000, 0x4000, 0x4000, 0xe000,   /* 1 */
  266.   0xe000, 0x1000, 0x6000, 0x8000, 0xf000,   /* 2 */
  267.   0xe000, 0x1000, 0x6000, 0x1000, 0xe000,   /* 3 */
  268.   0xa000, 0xa000, 0xf000, 0x2000, 0x2000,   /* 4 */
  269.   0xf000, 0x8000, 0xe000, 0x1000, 0xe000,   /* 5 */
  270.   0x6000, 0x8000, 0xe000, 0x9000, 0x6000,   /* 6 */
  271.   0xf000, 0x1000, 0x2000, 0x4000, 0x4000,   /* 7 */
  272.   0x6000, 0x9000, 0x6000, 0x9000, 0x6000,   /* 8 */
  273.   0x6000, 0x9000, 0x7000, 0x1000, 0x6000,   /* 9 */
  274.   0x6000, 0x9000, 0xf000, 0x9000, 0x9000,   /* A */
  275.   0xe000, 0x9000, 0xe000, 0x9000, 0xe000,   /* B */
  276.   0x7000, 0x8000, 0x8000, 0x8000, 0x7000,   /* C */
  277.   0xe000, 0x9000, 0x9000, 0x9000, 0xe000,   /* D */
  278.   0xf000, 0x8000, 0xe000, 0x8000, 0xf000,   /* E */
  279.   0xf000, 0x8000, 0xe000, 0x8000, 0x8000    /* F */
  280. };
  281.  
  282.  
  283. DisplayLabels()
  284. {
  285.    struct IntuiText Htext;
  286.    struct Image Himage;
  287.    int i, x, y;
  288.    char string[2];
  289.  
  290.    Himage.Width = Himage.Height = 5;
  291.    Himage.Depth = 1;
  292.    Himage.PlanePick = 0x1;
  293.    Himage.PlaneOnOff = 0x0;
  294.    Himage.NextImage = NULL;
  295.    Htext.IText      = string;
  296.    string[1]       = '\0';
  297.    Htext.ITextFont  = NULL;
  298.    Htext.FrontPen   = 1;
  299.    Htext.BackPen    = 0;
  300.    Htext.DrawMode   = JAM1;
  301.    Htext.NextText   = NULL;
  302.  
  303.    for(i=0, y=IMYTOP+10; i<256 && y < SHEIGHT-IHEIGHT; y+=IMYSKIP)
  304.    {
  305.       for(x=IMXLEFT-1; x < SWIDTH-IWIDTH && i<256; x +=IMXSKIP, i++)
  306.       {
  307.          if(i<33 || i > 126)
  308.          {
  309.             Himage.LeftEdge = x; Himage.TopEdge = y;
  310.             Himage.ImageData = heximagedata+(5*((0xf0 & (UBYTE) i) >> 4));
  311.             DrawImage(Window->RPort, &Himage, 0, 0);
  312.             Himage.LeftEdge += 6; Himage.TopEdge += 3;
  313.             Himage.ImageData = heximagedata+(5*((0x0f & (UBYTE) i)));
  314.             DrawImage(Window->RPort, &Himage, 0, 0);
  315.          }
  316.          else
  317.          {
  318.             Htext.LeftEdge = x; Htext.TopEdge = y; *string = (UBYTE) i;
  319.             PrintIText(Window->RPort, &Htext, 0, 0);
  320.          }
  321.       }
  322.    }
  323.  
  324.    return(0);
  325. }
  326.  
  327. panic(string, a1)
  328. char *string, *a1;
  329. {
  330.    clean(1);
  331.    printf(string, a1);
  332.    exit(1);
  333.    return(0);
  334. }
  335.  
  336. void main(argc, argv)
  337. int argc;
  338. char *argv[];
  339. {
  340.    struct IntuiMessage *imsg;
  341.    int i, which;
  342.    ULONG Class;
  343.    USHORT Code;
  344.    int MouseX, MouseY;
  345.  
  346. #ifdef MYDEBUG
  347.    if(argc > 1 && argv[1][0] == '-' && argv[1][1] == 'd')
  348.    {
  349.       debug = argv[1][2] - '0';
  350.       argc--; argv++;
  351.    }
  352.  #endif
  353.  
  354.    init();
  355.  
  356.    for(; argc > 1; argc--, argv++)
  357.    {
  358.       switch(argv[1][0])
  359.       {
  360.          case '-':
  361.             switch(argv[1][1])
  362.             {
  363.                case 'f':  /* characters filename specification */
  364.                case 'F':
  365.                   argc--; argv++;  /* FORCE the next parm to be name */
  366.                   if(argc > 1) LoadFont(argv[1], -1);
  367.                   else LoadFont("characters", -1);
  368.                   break;
  369.  
  370.                case 'h':  /* Hi-res mode */
  371.                case 'H':
  372.                   colgads[7].NextGadget = &colgads[8];
  373.                   hires = 1;
  374.                   break;
  375.  
  376.                default:
  377.                   printf("Unrecognized option '%s'\n", argv[1]);
  378.                   printf("Usage: HackIcon [-f] filename [ [-f] filename] [-h]\n");
  379.                   clean(1);
  380.                   exit(1);
  381.             }
  382.             break;
  383.  
  384.          default:
  385.             LoadFont(argv[1], -1);
  386.             break;
  387.       }
  388.    }
  389.  
  390.    BUG(1, ("main: Entry"))
  391.  
  392.    while(1)
  393.    {
  394.       if(!(imsg = (struct IntuiMessage *)GetMsg(Window->UserPort)))
  395.       {
  396.          WaitPort(Window->UserPort);
  397.          continue;
  398.       }
  399.       Class  = imsg->Class;
  400.       Code   = imsg->Code;
  401.       MouseX = imsg->MouseX;
  402.       MouseY = imsg->MouseY;
  403.       ReplyMsg(imsg);
  404.  
  405.       switch(Class)
  406.       {
  407.          case MOUSEBUTTONS:
  408.             BUG(5, ("main: case MOUSEBUTTONS\n"))
  409.             if(Code == SELECTDOWN)
  410.                which = WhichIcon(MouseX, MouseY);
  411.             else if(which >= 0 && which == WhichIcon(MouseX, MouseY))
  412.                EditIcon(which);
  413.             break;
  414.  
  415.          case MENUPICK:
  416.             BUG(5, ("main: case MENUPICK\n"))
  417.             if(Code == MENUNULL) break;
  418.  
  419.             BUG(8, ("main: menunum = %d\n", MENUNUM(Code)))
  420.             if((i = MENUNUM(Code)) > 0)
  421.             {
  422.                AboutHackicon();
  423.                break;
  424.             }
  425.  
  426.             BUG(8, ("main: itemnum = %d\n", ITEMNUM(Code)))
  427.             i = ITEMNUM(Code);
  428.             switch(i)
  429.             {
  430.                case M_LOAD:
  431.                   LoadFont(GetName(1), -1);
  432.                   break;
  433.  
  434.                case M_SAVE:
  435.                   SaveFont(NULL);
  436.                   break;
  437.  
  438.                case M_SAVEAS:
  439.                   SaveFont(GetName(0), -1);
  440.                   break;
  441.  
  442.                case M_COLORS:
  443.                   SetColors();
  444.                   break;
  445.  
  446.                case M_ALTFONT:
  447.                   SetFont();
  448.                   break;
  449.  
  450.                case M_QUIT:
  451.                   if(!clean(0)) exit(0);
  452.                   break;
  453.                   
  454.                case M_COPY:
  455.                   CopyIcon(0);
  456.                   break;
  457.  
  458.                case M_MCOPY:
  459.                   CopyIcon(1);
  460.                   break;
  461.  
  462.                default:
  463.                   break;
  464.             }
  465.             break;
  466.  
  467.          default:
  468.             break;
  469.       }
  470.    }
  471.    exit(1);
  472. }
  473.